package com.kkb.service;

        import com.kkb.dao.TransferDao;
        import com.kkb.util.DruidUtils;
        import org.apache.commons.dbutils.DbUtils;

        import java.sql.Connection;
        import java.sql.SQLException;

public class TransferService {
    TransferDao transferDao = new TransferDao();

    public void transferHandler(String outUser, String inUser, Double money) {
        Connection connection = null;
        try {
            // 获得数据库连接
            connection = DruidUtils.getConnection();
            // 开启事务
            connection.setAutoCommit(false);
            // 执行加钱的操作
            transferDao.in(connection, inUser, money);
            //int i = 100/0;    模拟异常
            // 执行减钱的操作
            transferDao.out(connection, outUser, money);
            // 提交事务
            DbUtils.commitAndCloseQuietly(connection);

        } catch (Exception e) {
            DbUtils.rollbackAndCloseQuietly(connection);
            //抛出异常
            throw new RuntimeException(e);
        }
    }
}
